//格式化时间
function dateFtt(fmt,date)
{ //author: meizz
	var o = {
		"M+" : date.getMonth()+1,                 //月份
		"d+" : date.getDate(),                    //日
		"h+" : date.getHours(),                   //小时
		"m+" : date.getMinutes(),                 //分
		"s+" : date.getSeconds(),                 //秒
		"q+" : Math.floor((date.getMonth()+3)/3), //季度
		"S"  : date.getMilliseconds()             //毫秒
	};
	if(/(y+)/.test(fmt))
		fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
	for(var k in o)
		if(new RegExp("("+ k +")").test(fmt))
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
	return fmt;
}
function formatSubscribeTime(cellValue, options, rowObject) {
	var dateStr = "";
	if(cellValue){
		var crtTime = new Date(cellValue);
		dateStr = dateFtt("yyyy-MM-dd hh:mm:ss",crtTime);
	}
	return dateStr;
}

//加载字典
function dictYyzt(value, options, row){
    //sex为自定义字典项
	var statusObj = vm.yyzt;
    for( var i=0;i< statusObj.length;i++ ){
        if(statusObj[i].value == value){
            return '<span>'+statusObj[i].text+'</span>' ;
        }
    }
}

function dictArriveState(value,options,row){
    if(value == '0'){
        return '<span>未到达</span>';
    }else if(value == '1'){
        return '<span>已到达</span>';
    }
}
$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'cartrial/reservationtime/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id_', width: 50, key: true,hidden:true },
			{ label: '检测点公司ID', name: 'companyId', index: 'company_id', width: 80,hidden:true },
			{ label: '检测点公司名称', name: 'companyName', index: 'company_name', width: 140 },
			{ label: '预约时间', name: 'reservationTime', index: 'reservation_time', width: 100,formatter:formatSubscribeTime },
			{ label: '预约状态', name: 'reservationState', index: 'reservation_state', width: 80 ,formatter:dictYyzt},
			{ label: '到达状态', name: 'arriveState', index: 'arrive_state', width: 80, formatter:dictArriveState},
			{ label: '用户编号', name: 'userid', index: 'userid', width: 80},
			{ label: '车牌号', name: 'licensePlate', index: 'license_plate', width: 80 }, 			
			{ label: '手机号', name: 'phone', index: 'phone', width: 80 }, 			
			{ label: '提交时间', name: 'createTime', index: 'create_time', width: 100,formatter:formatSubscribeTime },
			{ label: '1', name: 'state', index: 'state', width: 80,hidden:true },
			{ label: '修改时间', name: 'modifyTime', index: 'modify_time', width: 80,hidden:true }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		reservationTime: {},
        yyzt:[],
        queryCondition:{}
	},
    created: function(){
        $.ajax({
            type:"get",
            url: baseURL+"sys/dict/select/yyzt",
            success: function (data) {
                if(data.code===0){
                    vm.yyzt = data.dict.yyzt;
                }
            },
            error:function(XMLHttpRequest ,errormsg,catchException){
                alert(errormsg);
            }
        });

    },
    mounted:function(){
        $("#startTime,#endTime").datetimepicker({
            language:  'zh-CN', //语言设置
            format:'yyyy-mm-dd hh:ii:ss',
            todayBtn:true,
            todayHighlight:true
        });
    },
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.reservationTime = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.reservationTime.id == null ? "cartrial/reservationtime/save" : "cartrial/reservationtime/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.reservationTime),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "cartrial/reservationtime/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "cartrial/reservationtime/info/"+id, function(r){
				r.reservationTime.reservationTime = formatSubscribeTime(r.reservationTime.reservationTime);
				r.reservationTime.createTime = formatSubscribeTime(	r.reservationTime.createTime);
				r.reservationTime.modifyTime = formatSubscribeTime(	r.reservationTime.modifyTime);
                vm.reservationTime = r.reservationTime;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
            var postData = $("#jqGrid").jqGrid("getGridParam", "postData");
            $.each(postData, function (k, v) {
                delete postData[k];
            });
            var postData={
                "companyName":vm.queryCondition.companyName==""?undefined:$.trim(vm.queryCondition.companyName),
                "startTime":$("#startTime").val()==""?undefined:$.trim($("#startTime").val()),
                "endTime":$("#endTime").val()==""?undefined:$.trim($("#endTime").val()),
                "phone":vm.queryCondition.phone ==""?undefined:$.trim(vm.queryCondition.phone),
                "licensePlate":vm.queryCondition.licensePlate==""?undefined:$.trim(vm.queryCondition.licensePlate)
            }
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
                postData:postData
            }).trigger("reloadGrid");
		}
	}
});